package com.sgb.kjwl.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ShareSDKUtilsEntity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.util.MToast;

import java.util.HashMap;

import cn.sharesdk.dingding.utils.DingdingClientNotExistException;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;

/**
 * Created by：冯齐 on 2020/12/17 18:09
 * Description： 分享utils
 */
public class ShareSDKUtils {

    private static Platform qzone;

    public static void ShareShow(Context context, ShareSDKUtilsEntity shareBean) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(shareBean.getTitle());
        sp.setText(shareBean.getText());
        sp.setImageUrl(shareBean.getImageUrl());
        //在线URL为空，但有本地Bitmap时
        if (TextUtils.isEmpty(shareBean.getImageUrl()) && shareBean.getImageBitmap() != null) {
            sp.setImageData(shareBean.getImageBitmap());
        }
        sp.setUrl(shareBean.getUrl());
        sp.setShareType(Platform.SHARE_WEBPAGE);
        qzone = ShareSDK.getPlatform(shareBean.getShareType());//shareBean.getShareType()   Wechat.NAME    Dingding.NAME
        // 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
        qzone.setPlatformActionListener(new PlatformActionListener() {
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
                if (arg2 instanceof DingdingClientNotExistException) {
                    MToast.showToast(context, "您未安装钉钉客户端，分享失败！");
                }
                if (arg2 instanceof WechatClientNotExistException) {
                    MToast.showToast(context, "您未安装微信客户端，分享失败！");
                }
            }

            public void onComplete(Platform arg0, int arg1, HashMap arg2) {
                Log.e("tag", "分享成功................");
                //分享成功的回调
                if (shareBean.isToastShow()) {
                    videoDeleteDialog(context, shareBean.getToastText());
                }
            }

            public void onCancel(Platform arg0, int arg1) {
            }
        });
        // 执行图文分享
        qzone.share(sp);
    }

    private static void videoDeleteDialog(Context context, String toastText) {
        View view = View.inflate(context, R.layout.dialog_one_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tvtitle = view.findViewById(R.id.tv_title);
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        tvtitle.setText("分享成功");
        tv_message.setText("你可以在" + toastText + "里查看分享");
        //好的
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

}
